<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      left-arrow
      title="头条新闻"
      @click-left="$router.back()"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <div class="main-wrap">
      <!-- 加载中 -->
      <div class="loading-wrap" v-if="loading">
        <van-loading color="#3296fa" vertical>加载中</van-loading>
      </div>
      <!-- /加载中 -->
      <!-- 加载完成-文章详情 -->
      <div class="article-detail" v-else-if="article.title">
        <!-- 文章标题 -->
        <h1 class="article-title">{{ article.title }}</h1>
        <!-- /文章标题 -->
        <!-- 用户信息 -->
        <van-cell class="user-info" center :border="false">
          <van-image
            class="avatar"
            slot="icon"
            round
            fit="cover"
            :src="article.aut_photo"
          />
          <div slot="title" class="user-name">{{ article.aut_name }}</div>
          <div slot="label" class="publish-date">
            {{ article.pubdate | yearsage }}
          </div>
          <!-- 关注组件 -->
          <FollowUser
            :is_followed.sync="article.is_followed"
            :aut_id="article.aut_id"
          ></FollowUser>
          <!-- <van-button
            class="follow-btn"
            round
            size="small"
          >已关注</van-button> -->
        </van-cell>
        <!-- /用户信息 -->
        <!-- 文章内容 -->
        <div
          v-html="article.content"
          class="article-content markdown-body"
          @click="previewImg"
          ref="articleContent"
        ></div>
        <van-divider>正文结束</van-divider>
        <CommentList
          @information="onisReplyShow"
          :list="commentList"
          :art_id="article.art_id"
        ></CommentList>
        <van-popup
          closeable
          close-icon-position="top-left"
          v-model="isReplyShow"
          position="right"
          style="width: 100%; height: 100%"
        >
          <ReplyToComments
            :articleId="articleId"
            v-if="isReplyShow"
            :comment="comment"
          ></ReplyToComments>
          <!-- <AddComment
        @post-success="onpostSuccess"
        :target="article.art_id"
      ></AddComment> -->
        </van-popup>
        <!-- 底部 -->
        <BottomArea
          @onpostSuccess="onpostSuccess"
          v-if="article.title"
          :article="article"
        ></BottomArea>
      </div>
      <!-- /加载完成-文章详情 -->

      <!-- 加载失败：404 -->
      <div class="error-wrap" v-else-if="errStatus">
        <van-icon name="failure" />
        <p class="text">该资源不存在或已删除！</p>
      </div>
      <!-- /加载失败：404 -->

      <!-- 加载失败：其它未知错误（例如网络原因或服务端异常） -->
      <div class="error-wrap" v-else>
        <van-icon name="failure" />
        <p class="text">内容加载失败！</p>
        <van-button class="retry-btn" @click="loadData">点击重试</van-button>
      </div>
      <!-- /加载失败：其它未知错误（例如网络原因或服务端异常） -->
    </div>
  </div>
</template>

<script>
import FollowUser from '@/components/FollowUser'
import CommentList from './CommentList'
import BottomArea from './BottomArea'
import ReplyToComments from './ReplyToComments'
import { getArticleDetails } from '@/api/article'
import { ImagePreview } from 'vant'
export default {
  name: 'Article',
  data() {
    return {
      article: {},
      loading: true, // 判断有无数据
      errStatus: false, // 判断失败原因
      isReplyShow: false,
      commentList: [],
      comment: null
    }
  },
  components: {
    FollowUser,
    CommentList,
    BottomArea,
    ReplyToComments
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  activated() {
    this.loadData()
  },
  deactivated() {
    this.commentList = null
    this.loading = true
  },
  methods: {
    // 获取文章详情
    async loadData() {
      try {
        const { data } = await getArticleDetails(this.articleId)
        this.article = data
        this.loading = false
        console.log(this.article)
      } catch (error) {
        this.loading = false
        if (error.response.status === 404) {
          this.errStatus = true
        }
      }
    },
    // 点击预览图片
    previewImg(e) {
      const imgs = [...this.$refs.articleContent.querySelectorAll('img')]
      if (imgs.includes(e.target)) {
        const images = imgs.map((item) => item.src)
        const startPosition = images.findIndex((item) => item === e.target.src)
        // 图片预览
        ImagePreview({
          images,
          startPosition
        })
      }
    },
    onpostSuccess(data) {
      this.commentList.unshift(data.new_obj)
      this.article.comm_count++
    },
    // 回复评论
    onisReplyShow(comment) {
      this.isReplyShow = true
      this.comment = comment
    }
  }
}
</script>

<style scoped lang="less">
@import './github-markdown.css';
.article-container {
  /deep/.van-nav-bar {
    .van-icon {
      color: #333;
    }
  }
  .main-wrap {
    position: fixed;
    z-index: 2;
    left: 0;
    right: 0;
    top: 92px;
    bottom: 88px;
    overflow-y: scroll;
    background-color: #fff;
  }
  .article-detail {
    .article-title {
      font-size: 40px;
      padding: 50px 32px;
      margin: 0;
      color: #3a3a3a;
    }
    .user-info {
      padding: 0 32px;
      .avatar {
        width: 70px;
        height: 70px;
        margin-right: 17px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 24px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 23px;
        color: #b7b7b7;
      }
    }

    .article-content {
      padding: 55px 32px;
      /deep/ p {
        text-align: justify;
      }
    }
  }

  .loading-wrap {
    padding: 200px 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .error-wrap {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      font-size: 30px;
      color: #666666;
      margin: 33px 0 46px;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666666;
    }
  }
}
</style>
